package com.example.springpethospital.service;

import com.example.springpethospital.entity.*;
import com.example.springpethospital.mapper.IllcaseFileMapper;
import com.example.springpethospital.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

@Service
public class IllcaseFileServiceImpl implements IllcaseFileService {
    @Autowired
    IllcaseFileMapper illcaseFileMapper;

    @Override
    public List<IDAndName> getFileList(int caseID, CaseType casetype, FileType filetype) {
        return illcaseFileMapper.getFileList(caseID, casetype.toInteger(), filetype.toInteger());
    }

    @Override
    public FileAndFiletype getFile(int fileID) {
        String filename = illcaseFileMapper.getFilepath(fileID);
        if (filename != null && !filename.isEmpty()) {
            String pathname = Const.FILE_PREFIX + filename;
            FileAndFiletype ret = new FileAndFiletype();
            ret.file = new File(pathname);
            ret.fileType = FileType.toFileType(illcaseFileMapper.getFiletype(fileID));
            return ret;
        } else {
            return null;
        }
    }

    private String saveFile(MultipartFile file) throws IOException {
        String oriFilename = file.getOriginalFilename();
        String renamedFilename = Utility.getRenamedFileName(oriFilename);
        if (renamedFilename != null) {
            String pathname = Const.FILE_PREFIX + renamedFilename;
            file.transferTo(Paths.get(pathname));
            return renamedFilename;
        } else {
            throw new IOException("文件名为空");
        }
    }

    @Override
    public void AddFile(int caseID, CaseType casetype, FileType filetype, MultipartFile file) throws IOException, IllegalArgumentException {
        String filename = saveFile(file);
        boolean success = illcaseFileMapper.AddFile(caseID, casetype.toInteger(), filetype.toInteger(), filename);
        if (!success) throw new IllegalArgumentException("项目id不存在");
    }

    @Override
    public boolean DeleteFile(int fileID) {
        String pathname = Const.FILE_PREFIX + illcaseFileMapper.getFilepath(fileID);
        File file = new File(pathname);
        return illcaseFileMapper.RemoveFile(fileID) && file.delete();
    }

    @Override
    public int DeleteFileByCase(int caseID) {
        List<Integer> filelist = illcaseFileMapper.getFileIDsByCaseID(caseID);
        int cnt = 0;
        for (Integer fileID : filelist) {
            cnt += DeleteFile(fileID) ? 1 : 0;
        }
        return cnt;
    }

    @Override
    public void modifyFileProperties(int fileID, int caseID, CaseType casetype, FileType filetype) throws IllegalArgumentException {
        boolean success = illcaseFileMapper.ModifyFileProperties(fileID, caseID, casetype.toInteger(), filetype.toInteger());
        if (!success) throw new IllegalArgumentException("文件id不存在");
    }
}
